<?php

class myutils
{
    static $rest2db = array(
        'idUser' => 'id',
        'idJourney' => 'id',
        'journeyStatus' => 'journey_status',
        'departureLat' => 'departure_lat',
        'departureLng' => 'departure_lng',
        'arrivaLat' => 'arriva_lat',
        'arrivaLng' => 'arriva_lng',
        'departureDate' => 'departure_date',
        'departureTime' => 'departure_time',
        'passengers' => 'passengers',
        'luggages' => 'luggages',
        'owner' => 'fl_owner',
        'username' => 'username',
        'gender' => 'gender',
        'birthDate' => 'birth_date'
    );

    static public $conn_string = "host=postgresql.alwaysdata.com dbname=mytaxisharing_prova user=mytaxisharing_ospite password=ospite";

    public static function translate_rest_params($rest_param)
    {
        $db_handle = pg_pconnect(myutils::$conn_string);
        foreach ($rest_param as $key => $val) {
            if (isset(myutils::$rest2db[$key]))
                $result0[myutils::$rest2db[$key]] = pg_escape_string($db_handle, $val);
            else
                $result1[$key] = pg_escape_string($db_handle, $val);
        }
        pg_close($db_handle);
        return array($result0, $result1);
    }

    public static function dbresult_to_json($result)
    {
        $numrows = pg_numrows($result);
        if ($numrows > 0) {
            $jsonData = array();
            for ($row = 0; $row < $numrows; $row++) {
                $jsonData[] = json_encode(pg_fetch_array($result, $row, PGSQL_ASSOC));
            }
            return "[" . implode(", ", $jsonData) . "]";
        } else
            return "[]";
    }
}

?>